﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FTBS.Models.BLO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using PagedList;

namespace FTBS.Controllers
{
    public class SettingTheaterController : Controller
    {

        private TheaterBLO _theaterBlo = new TheaterBLO();

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page, bool? isSortDescending)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            List<Theater> listTheater = new List<Theater>();
            listTheater = _theaterBlo.GetAllTheaterInfo();

            ViewBag.NameSortParm = "theater_desc";
            ViewBag.CurrentSort = sortOrder;

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            ViewBag.IsSortDes = false;

            if (!String.IsNullOrEmpty(searchString))
            {
                listTheater = listTheater.Where(setting => setting.TheaterName.ToUpper().Contains(searchString.ToUpper())
                                                  || setting.MaxChairInRow.ToString().ToUpper().Contains(searchString.ToUpper())
                                                  || setting.MaxColumnInRoom.ToString().ToUpper().Contains(searchString.ToUpper())
                                                  || setting.MaxBookingTicket.ToString().ToUpper().Contains(searchString.ToUpper())
                                                  || setting.DeadlineCreateTicket.ToString().ToUpper().Contains(searchString.ToUpper())
                                                  || setting.DeadlineForBooking.ToString().ToUpper().Contains(searchString.ToUpper())).ToList();
            }

            switch (sortOrder)
            {
                case "theaterName_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.TheaterName).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.TheaterName).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "maxChairInRow_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.MaxChairInRow).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.MaxChairInRow).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "maxColumn_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.MaxColumnInRoom).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.MaxColumnInRoom).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "maxBookingTicket_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.MaxBookingTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.MaxBookingTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "deadlineForCreateTicket_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.DeadlineCreateTicket).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.DeadlineCreateTicket).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "pointWithMoney_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.PointWithMoney).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.PointWithMoney).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                case "deadlineForBooking_desc":
                    if (isSortDescending == true)
                    {
                        listTheater = listTheater.OrderByDescending(setting => setting.DeadlineForBooking).ToList();
                        ViewBag.IsSortDes = false;
                    }
                    else
                    {
                        listTheater = listTheater.OrderBy(setting => setting.DeadlineForBooking).ToList();
                        ViewBag.IsSortDes = true;
                    }
                    break;
                default:
                    listTheater = listTheater.OrderBy(setting => setting.TheaterName).ToList();
                    ViewBag.IsSortDes = false;
                    break;
            }

            int pageSize = 5;
            int pageNumber = (page ?? 1);

            return View(listTheater.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult Edit(int id)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            Theater theater = _theaterBlo.GetTheaterById(id);
            if (theater == null)
            {
                return HttpNotFound();
            }
            return View(theater);
        }

        [HttpPost]
        public ActionResult Edit(Theater theater)
        {
            if (this.Session["RoleName"] == null || this.Session["UserName"] == null || this.Session["Account"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return Redirect("/Home/Announcement");
            }
            if (ModelState.IsValid)
            {
                _theaterBlo.EditTheater(theater);
                return RedirectToAction("Index");
            }
            return View(theater);
        }
    }
}
